package BasicClass;

import java.util.Arrays;
import java.util.Comparator;

public class comparator {

    public static void main(String[] args) {

//        Integer[] arr = {473,423,119,128, 63, 85, 377, 34, 481, 359, 472, 321, 154, 267, 169, 9, 187, 170, 204, 273 };
//        Arrays.sort(arr,new MyComp());
//        for (Integer integer : arr) {
//            System.out.print(integer+" ");
//        }
//        //输出展示：481 473 472 423 377 359 321 273 267 204 187 170 169 154 128 119 85 63 34 9

        Student student1 = new Student("风", 2, 6000);
        Student student2 = new Student("雨", 1, 5000);
        Student student3 = new Student("雷", 2, 6050);
        Student student4 = new Student("电", 1, 7000);

        Student[] arr = {student1,student2,student4,student3};

        Arrays.sort(arr,new MyCopStu());

        for (Student student : arr) {
            System.out.println(student.name+" ,"+student.classNo+" ,"+student.age);
        }
        /**
         * 控制台输出：
         *  雷 ,2 ,6050
         *  风 ,2 ,6000
         *  电 ,1 ,7000
         *  雨 ,1 ,5000
         */
    }

    public static class Student{
        String name;
        Integer classNo;
        Integer age;

        public Student(String name, Integer classNo, Integer age) {
            this.name = name;
            this.classNo = classNo;
            this.age = age;
        }
    }

    public static class MyComp implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    }

    public static class MyCopStu implements Comparator<Student>{

        /**
         * 先按照班级进行从小到大排列，同班级按照年龄从大到小排列
         * @param o1
         * @param o2
         * @return
         */
        @Override
        public int compare(Student o1, Student o2) {
            if(o1.classNo == o2.classNo){
                return o2.age - o1.age;
            }
            return o2.classNo - o1.classNo;
        }
    }
}
